﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Utilities.ExpressionEval;

namespace SimpleServiceBus.Bus.MessageManagement.MessageValueProviders
{
    public class ExpressionMessageValueProvider : IMessageValueProvider
    {
        public ExpressionMessageValueProvider(string expression)
        {
            ExpressionString = expression;
        }

        public string ExpressionString { get; set; }

        public string GetMessageValue(MessageEnvelope message, IMessageContext messageContext)
        {
            if (string.IsNullOrEmpty(ExpressionString))
                return String.Empty;

            var eval = messageContext.GetLocalService<IMessageExpressionEvaluator>();
            if (eval == null)
                throw new InvalidOperationException(
                    "No Expression Evaluator has been configured to evaluate the expression for this value provider.");

            return eval.Evaluate<string>(ExpressionString, message);
        }
    }
}
